package com.yf.document.modules.flowable.model.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.boot.base.api.api.ApiRest;
import com.yf.boot.base.api.api.controller.BaseController;
import com.yf.boot.base.api.api.dto.BaseIdReqDTO;
import com.yf.boot.base.api.api.dto.BaseIdsReqDTO;
import com.yf.boot.base.api.api.dto.PagingReqDTO;
import com.yf.boot.base.api.utils.DateUtils;
import com.yf.document.modules.flowable.model.dto.AcdeModelDTO;
import com.yf.document.modules.flowable.model.service.AcdeModelService;
import com.yf.document.modules.user.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* <p>
* 控制器
* </p>
*
* @author Panjp
* @since 2022-11-04 10:34
*/
@Api(tags={""})
@RestController
@RequestMapping("/api/flowable/model")
public class AcdeModelController extends BaseController {

    @Autowired
    private AcdeModelService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody AcdeModelDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
     * 发布流程
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "发布流程")
    @RequestMapping(value = "/fbFlow", method = { RequestMethod.POST})
    public ApiRest fbFlow(@RequestBody AcdeModelDTO reqDTO) {
        AcdeModelDTO dto = baseService.detail(reqDTO.getId());
        dto.setModelStatus("1");
        baseService.save(dto);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.delete(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<AcdeModelDTO> detail(@RequestBody BaseIdReqDTO reqDTO) {
        AcdeModelDTO dto = baseService.detail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 新建流程初始化流程信息
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "新建流程初始化流程信息")
    @RequestMapping(value = "/initFlowDetail", method = { RequestMethod.POST})
    public ApiRest<AcdeModelDTO> initFlowDetail(@RequestBody BaseIdReqDTO reqDTO) {
        AcdeModelDTO dto = baseService.detail(reqDTO.getId());
        dto.setCreateByName(UserUtils.getUser(false).getRealName());
        dto.setDateTime(DateUtils.formatDate(new Date(), "yyyyMMddHHmmss"));
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<AcdeModelDTO>> paging(@RequestBody PagingReqDTO<AcdeModelDTO> reqDTO) {

        //分页查询并转换
        IPage<AcdeModelDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiRest<List<AcdeModelDTO>> list(@RequestBody AcdeModelDTO reqDTO) {
        // 查找列表
        List<AcdeModelDTO> dtoList = baseService.list(reqDTO);
        return super.success(dtoList);
    }
}
